package com.sy.service.impl;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sy.dto.PageBean;
import com.sy.entity.HomeFilePath;
import com.sy.entity.Homework;
import com.sy.entity.HomeworkFilePath;
import com.sy.entity.Student;
import com.sy.entity.Teacher;
import com.sy.exception.BusinessException;
import com.sy.mapper.*;
import com.sy.service.HomeWorkService;
import com.sy.vo.HomeworkUpdateVo;
import com.sy.vo.HomeworkVo;
import com.sy.vo.StudentHomeVo;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.TimeZone;
import java.util.stream.Collectors;

import static com.sy.constant.BusinessConstant.*;

/**
 * @Author:fma
 * @Date: 2021/10/12 11:11
 */
@Service
public class HomeWorkServiceImpl implements HomeWorkService {
    @Autowired
    private HomeWorkMapper homeWorkMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private HomeworkFilePathMapper homeworkFilePathMapper;

    @Autowired
    private StudentHomeMapper studentHomeMapper;

    @Override
    public List<Homework> getAllHomes(Integer startLimit, Integer endLimit) {
        PageHelper.startPage(startLimit, endLimit);
        List<Homework> allHomes = homeWorkMapper.getAllHomes();
        if (Objects.isNull(allHomes)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "得到所有的作业失败");
        }
        return allHomes;
    }

    @Override
    public PageBean<Homework> getAllHomesBySno(Integer sno, Integer pageNo, Integer pageNum) {
        if (Objects.isNull(sno)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "学生学号不能为空");
        }
        //单个学生信息
        Student student = studentMapper.getStuPwdByName(sno);
        if (Objects.isNull(student)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "该学生不存在...");
        }
        //该学生的班级
        String className = student.getClassName();
        //该学生的年级
        String grade = student.getGrade();
        Homework homework=new Homework();
        homework.setClassName(className);
        homework.setGrade(grade);
        PageHelper.startPage(pageNo,pageNum);
        Page<Homework> page = (Page<Homework>) homeWorkMapper.getAllHomesBySno(homework);
        //将Page封装为自定义的分页返回类型
        PageBean<Homework> pageBean = new PageBean<>();
        //页码能超过最大值
        int pageCount = page.getPages();
        pageBean.setPageNo(page.getPageNum());
        pageBean.setPageCount(pageCount);
        pageBean.setPageSize(pageNum);
        pageBean.setTotal(page.getTotal());
        System.out.println("这是模糊查询后的总数量"+pageBean.getTotal());
        List<Homework> products=page.getResult();
        pageBean.setRecords(products.stream().collect(Collectors.toList()));
        return pageBean;
    }

    @Override
    public Integer getAllHomes() {
        List<Homework> allHomes = homeWorkMapper.getAllHomes();
        Integer size = allHomes.size();
        if (Objects.isNull(size)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "得到所有的作业的数量失败");
        }
        return size;
    }

    @Override
    public List<Homework> getHomeByTno(Integer tno) {
        if (Objects.isNull(tno)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "教师学号不能为空");
        }
        List<Homework> homeByTno = homeWorkMapper.getHomeByTno(tno);
        if (Objects.isNull(homeByTno)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "得到当前指定教师所有的作业失败");
        }
        return homeByTno;
    }

    @Override
    public Homework getHomeById(Integer id) {
        if (Objects.isNull(id)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "作业的id不能为空");
        }
        Homework homeById = homeWorkMapper.getHomeById(id);
        if (Objects.isNull(homeById)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "得到当前作业id找到指定的作业失败");
        }
        return homeById;
    }

    @Override
    public boolean isHomeTitleExists(String title) {
        if (title.isEmpty()){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "发布标题不能为空");
        }
        if(Objects.isNull(homeWorkMapper.getHomeworkByTitle(title))){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "作业标题不存在");
        }
        return true;
    }

    @Override
    public boolean publishHomework(HomeworkVo homeworkvo) {
        String className = homeworkvo.getClassName();
        Integer tno = homeworkvo.getTno();
        String title = homeworkvo.getTitle();
        String content = homeworkvo.getContent();
        String remark = homeworkvo.getRemark();
        String grade = homeworkvo.getGrade();
        if (Objects.isNull(className)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "发布班级不能为空");
        }
        if (Objects.isNull(tno)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "发布教师不能为空");
        }
        if (Objects.isNull(title)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "发布标题不能为空");
        }
        if (Objects.isNull(grade)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "发布年级不能为空");
        }
        if (Objects.isNull(content)){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "发布作业内容不能为空");
        }
        if(!Objects.isNull(homeWorkMapper.getHomeworkByTitle(title))){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "作业标题不能重复");
        }

        Homework homework = new Homework(null,tno,title,content,className,grade,remark,null,new DateTime());
        boolean success = homeWorkMapper.publishHomework(homework);
        List<Student> students = studentMapper.queryStudentsByGradeAndClassName(homeworkvo.getGrade(), homeworkvo.getClassName());
        for (Student student:students){
            studentHomeMapper.addStudentHomework
                    (homework.getId(),STUDENT_HOMEWORK_STATE_NO,student.getSno(),homework.getCreateDate());
        }
        for (String filePath : homeworkvo.getFilepath()) {
            HomeworkFilePath homeworkFilePath = new HomeworkFilePath();
            homeworkFilePath.setHomeworkid(homework.getId());
            homeworkFilePath.setFilePath(filePath);
            homeworkFilePathMapper.addHomeworkFilePath(homeworkFilePath);
        }
        return success;
    }

    //数据库更新的代码...
    @Override
    public PageBean<Homework> getHomeWorksByConditions(Integer sno,String theName, String theTitle, Integer pageNo, Integer pageNum) {
        if (Objects.isNull(sno)){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "学生编号不能都为空");
        }
        Student student = studentMapper.getStuPwdByName(sno);
        if (Objects.isNull(student)){
            throw new BusinessException(HttpStatus.NOT_FOUND, "该学生未存在...");
        }
        String className = student.getClassName();
        String grade = student.getGrade();
        if (StringUtils.isBlank(theName) && StringUtils.isBlank(theTitle)){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "教师姓名或者作业标题不能都为空");
        }
        Integer tno=null;
        if (!StringUtils.isBlank(theName)){
            Teacher teacher = teacherMapper.getTeacherByName(theName);
            if (Objects.isNull(teacher)){
                throw new BusinessException(HttpStatus.NOT_FOUND, "该教师未存在...");
            }
            //教师的编号
            tno = teacher.getTno();
        }
        Homework homework=new Homework();
        homework.setTitle(theTitle);
        homework.setClassName(className);
        homework.setGrade(grade);
        PageHelper.startPage(pageNo, pageNum);
        Page<Homework> page = (Page<Homework>)homeWorkMapper.getHomeWorksByConditions(className, grade, tno, theTitle);
        PageBean<Homework> pageBean = new PageBean<>();
        int pageCount = page.getPages();
        pageBean.setPageNo(page.getPageNum());
        pageBean.setPageCount(pageCount);
        if (!StringUtils.isBlank(theTitle)){
            List<Homework> theHomework = homeWorkMapper.getHomeworkByTitleName(homework);
            if (theHomework.isEmpty()){
                throw new BusinessException(HttpStatus.NOT_FOUND, "该作业未存在或该班未选择该教师...");
            }
        }
        pageBean.setPageSize(pageNum);
        pageBean.setTotal(page.getTotal());
        List<Homework> products=page.getResult();
        pageBean.setRecords(products.stream().collect(Collectors.toList()));
        return pageBean;
    }

    @Override
    public void selectHomeworkByTitle(String title) {
        Homework homework= homeWorkMapper.selectHomeworkByTitle(title);
        if(!Objects.isNull(homework)){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "作业的标题已经存在");
        }
    }

    @Override
    public boolean deleteHomeworkById(Integer id) {
        if(homeWorkMapper.deleteHomeworkById(id)){
            homeworkFilePathMapper.deleteHomeworkPathById(id);
            studentHomeMapper.deletePublishHomeworks(id);
            return true;
        }else{
            throw new BusinessException(HttpStatus.BAD_REQUEST, "删除作业发生异常");
        }
    }

    @Override
    public PageBean<Homework> getHomeWorksByConditions2(Integer tno,String title, Integer pageNo, Integer pageNum) {
        //条件必须有一个
        if (tno == null) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "教师编号tno不能为空");
        }
        //页码不能小于1
        if (pageNo <= 0) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "页码必须至少为1");
        }
        PageHelper.startPage(pageNo, pageNum);
        Page<Homework> page = (Page<Homework>) homeWorkMapper.getHomeByConditions2( tno,title );
        //将Page封装为自定义的分页返回类型
        PageBean<Homework> pageBean = new PageBean<>();
        //页码能超过最大值
        int pageCount = page.getPages();
        pageBean.setPageNo(page.getPageNum());
        pageBean.setPageCount(pageCount);
        pageBean.setPageSize(pageNum);
        pageBean.setTotal(page.getTotal());
        System.out.println("这是模糊查询后的总数量"+pageBean.getTotal());
        List<Homework> products=page.getResult();
        pageBean.setRecords(products.stream().collect(Collectors.toList()));
        return pageBean;
    }

    @Override
    public List<String>  getHomeFilePathsByHid(Integer hid) {
        if (Objects.isNull(hid)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "作业的id不能为空");
        }
        return homeworkFilePathMapper.getHomeFilePathByHid(hid);
    }


    @Override
    public boolean updateHomeWork(HomeworkUpdateVo homeworkUpdateVo) {
        return homeWorkMapper.updateHomeWork(homeworkUpdateVo);
    }

    @Override
    public boolean updateHomeWorkMarks(Integer id, Integer mark) {
        return homeWorkMapper.updateHomeWorkMarks(id,mark);
    }

    @Override
    public List<String> getHomeFilePathsByHid2(Integer hid, Integer sno) {
        return homeWorkMapper.getHomeFilePathsByHid2(hid,sno);
    }
}
